package operations;

import aboutTheBook.BookShelf;

import java.util.Scanner;

public class Return implements IOperation,IFind{
    @Override
    public int indexOf(String toFind, BookShelf bookShelf) {
        //若是书架为空，直接返回-1
        if(bookShelf.getSize()==0) {
            return -1;
        }
        for(int i = 0; i < bookShelf.getSize(); i++) {
            //使用书名进行查找
            if(bookShelf.getBookList()[i].getTitle().equals(toFind)) {
                return i;
            }
        }
        return -1;
    }
    @Override
    public void work(BookShelf bookShelf) {
        System.out.println("请输入您需要归还的书名：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int ret = indexOf(name,bookShelf);
        if(ret<0) {
            System.out.println("抱歉，您需要归还的书籍不存在！");
        } else {
            if(bookShelf.getBookList()[ret].getLoanStatus()) {
                bookShelf.getBookList()[ret].setLoanStatus(false);
                System.out.println("归还成功！");
            } else {
                System.out.println("抱歉，此书未被借出，无需归还。");
                System.out.println("请检查书名是否正确！");
            }
        }
    }
}
